package com.borya.service.mobile.impl;

import com.alibaba.fastjson.JSONObject;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.util.ObjectUtils;
import com.borya.model.db.User;
import com.borya.service.mobile.MerchantService;
import com.borya.service.mobile.UploadImgFileService;
import com.borya.service.mobile.UserService;
import com.borya.statuscode.StatusCode;
import com.borya.util.md5.MD5Util;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Component("uploadImgFileService")
public class UploadImgFileServiceImpl implements UploadImgFileService {

    private Logger log = Logger.getLogger(getClass());
    @Autowired("userServiceImpl")
    private UserService userService;
    @Autowired("merchantServiceImpl")
    private MerchantService merchantService;

    @Override
    public String upload(User user, ServerRequest request, JSONObject params) {
        try {
            File file = request.fileUploads().get(0).getFile();
            log.info("文件流上传，文件名为："+ file.getName());
            //判断图片一致性，MD5
            String md5 = params.getString("md5");
            String fileMD5 = MD5Util.getFileMD5(file);
            if(!fileMD5.equals(md5)){
                //图片被替换
                return StatusCode.PARAM_ERROR.toJSON("图片MD5校验不一致，图片错误");
            }
            String img =tansformFileToBase64(file);
            params.put("img",img);
            String uploadType = params.getString("uploadType");
            if (ObjectUtils.isNullOrEmpty(uploadType)) {
                //参数错误，缺少uploadType
                return StatusCode.PARAM_ERROR.toJSON("参数错误，缺少uploadType");
            }
            if ("1".equals(uploadType) || "3".equals(uploadType)) {
                //  c/user/uploadImg
                String result = userService.uploadImg(user, params);
                params.put("img", "");
                log.info("OUT[" + result + "]");
                return result;
            } else if ("2".equals(uploadType)) {
                // ums/c/merchant/shqpSign
                String r = merchantService.setImg(user, params, 1);
                params.put("img", "");
                log.info("get return : " + r);
                return r;
            }  else {
                return StatusCode.PARAM_ERROR.toJSON("参数错误，缺少uploadType");
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return StatusCode.SYSTEM_EXCEPTION.toJSON();
        }finally {
            System.gc();
        }
    }
    private String tansformFileToBase64(File file){
        if(file==null) {
            return null;
        }
        String base64 = null;
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
            byte[] buff = new byte[fin.available()];
            fin.read(buff);
            base64 = Base64.encode(buff);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return base64;
    }
}
